# -*- coding: utf-8 -*-
'''
Created on 2020/1/1 12:56
author:dyx
IDE:PyCharm
'''
import numpy as np

np.random.seed(1337)  # for reproducibility (每次生成的随机数相同)

from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model

# create some data
X = np.linspace(-1, 1, 200)
np.random.shuffle(X)
Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200,))
X_train, Y_train = X[:150], Y[:150]
X_test, Y_test = X[150:], Y[150:]
model = Sequential()
model.add(Dense(units=1, input_dim=1))
model.compile(loss='mse', optimizer='sgd')
# training
for step in range(301):
    cost = model.train_on_batch(X_train, Y_train)#Keras有很多开始训练的函数，这里用train_on_batch（）
#save
print('test before save:',model.predict(X_test[0:2]))
model.save('regessor_example.h5')
del model

#load
model = load_model('regessor_example.h5')
print('test after load:',model.predict(X_test[0:2]))


'''
# test
print('\nTesting ------------')
cost = model.evaluate(X_test, Y_test, batch_size=40)
print('test cost:', cost)
W, b = model.layers[0].get_weights()# 查看训练出的网络参数
                                        # 由于我们网络只有一层，且每次训练的输入只有一个，输出只有一个
                                        # 因此第一层训练出Y=WX+B这个模型，其中W,b为训练出的参数
print('Weights=', W, '\nbiases=', b)

# plotting the prediction
Y_pred = model.predict(X_test)
'''